<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------

namespace Admin\Controller;

use Admin\Api\DatatreeApi;
use Admin\Api\OrgMemberApi;
use Admin\Model\MessageModel;
use Common\Api\ExchangeLogApi;
use Shop\Api\AlipayNotifyApi;
use Shop\Api\OrdersContactInfoApi;
use Shop\Api\ProductFaqApi;
use Shop\Api\WxpayNotifyApi;
use Shop\Api\OrderRefundMoneyApi;
use Shop\Api\OrdersApi;
use Shop\Api\OrdersExpressApi;
use Shop\Api\OrdersInfoViewApi;
use Shop\Api\OrdersItemApi;
use Shop\Api\OrderStatusApi;
use Shop\Api\OrderStatusHistoryApi;
use Shop\Model\OrdersModel;
use Shop\Api\OrderRefundApi;
use Think\Hook;
use Api\Controller\WxpayAppController;
use Tool\Api\CityApi;
use Tool\Api\ProvinceApi;
use Uclient\Api\UserApi;


class WxWebOrdersController extends AdminController
{
    /**
     * 初始化
     */
    protected function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 订单管理
     */
    public function ordersmanage() {
        $arr = getDataRange(3);
        $payStatus = I('paystatus', '');
        $orderStatus = I('orderstatus', '');
        $order_code = I('post.order_code', '');
        $userid = I('uid', 0);
        $startdatetime = urldecode($arr[0]);
        //I('startdatetime', , 'urldecode');
        $enddatetime = urldecode($arr[1]);
        //I('enddatetime',   , 'urldecode');

        //分页时带参数get参数
        $params = array('startdatetime' => $startdatetime, 'enddatetime' => ($enddatetime),'wxaccountid'=>getWxAccountID());

        $startdatetime = strtotime($startdatetime);
        $enddatetime = strtotime($enddatetime);

        if ($startdatetime === FALSE || $enddatetime === FALSE) {
            LogRecord('INFO:' . $result['info'], '[FILE] ' . __FILE__ . ' [LINE] ' . __LINE__);
            $this -> error(L('ERR_DATE_INVALID'));
        }

        $map = array();
        //$map['wxaccountid'] = getWxAccountID();
        if (!empty($order_code)) {
            $map['order_code'] = array('like', $order_code . '%');

        }
        if ($payStatus != '') {
            $map['pay_status'] = $payStatus;
            $params['paystatus'] = $payStatus;
        }
        if ($orderStatus != '') {
            $map['order_status'] = $orderStatus;
            $params['orderstatus'] = $orderStatus;
        }
        $map['createtime'] = array( array('EGT', $startdatetime), array('elt', $enddatetime), 'and');

        $page = array('curpage' => I('get.p', 0), 'size' => C('LIST_ROWS'));
        $order = " createtime desc ";

        if ($userid > 0) {
            $map['uid'] = $userid;
        }

        //地区管理员所属地区限制
        $AreaMap = $this->OrgAreaMap();
        if(!empty($AreaMap)) $map['_complex'] = $AreaMap;
        $result = apiCall(OrdersInfoViewApi::QUERY, array($map, $page, $order, $params));

        //
        if ($result['status']) {
            $this -> assign('order_code', $order_code);
            $this -> assign('orderStatus', $orderStatus);
            $this -> assign('payStatus', $payStatus);
            $this -> assign('startdatetime', $startdatetime);
            $this -> assign('enddatetime', $enddatetime);
            $this -> assign('list', $result['info']['list']);
            $this -> assign('show', $result['info']['show']);
            $this -> display();
        } else {
            LogRecord('INFO:' . $result['info'], '[FILE] ' . __FILE__ . ' [LINE] ' . __LINE__);
            $this -> error($result['info']);
        }
    }
    /**
     * 地区管理员地区集map
     */
    private function OrgAreaMap(){

        $permisson = R('OrgArea/check_manager_permisson',array(),'Widget');
        $this->assign('permisson',$permisson);
        $AreaMap = array();

        if($permisson != 7) return $AreaMap;//不是地区管理员

        $cityid = array();
        $map['type'] = 1;
        $map['member_uid'] = UID;
        $result = apiCall(OrgMemberApi::QUERY_NO_PAGING,array($map));
        if($result['status']){
            foreach($result['info'] as $val){
                array_push($cityid,$val['organization_id']);
            }

        }

        $provinceid = array();
        $map['type'] = 0;
        $result = apiCall(OrgMemberApi::QUERY_NO_PAGING,array($map));
        if($result['status']){
            foreach($result['info'] as $val){
                array_push($provinceid,$val['organization_id']);
            }

        }
        $cityid = implode(',',$cityid);
        $provinceid = implode(',',$provinceid);


        $city = array(-1);
        $province = array(-1);
        //查询地区名称
        if(!empty($cityid)){
            $map = "cityid in($cityid)";
            $filed = 'city';
            $result = apiCall(CityApi::QUERY_NO_PAGING,array($map,false,$filed));
            if($result['status']) $city = array_column($result['info'],'city');
        }


        if(!empty($provinceid)){
            $map = "provinceid in($provinceid)";
            $filed = 'province';
            $result = apiCall(ProvinceApi::QUERY_NO_PAGING,array($map,false,$filed));
            if($result['status']) $province = array_column($result['info'],'province');
        }


        $AreaMap = array(
            'city' => array('in',$city),
            'province' => array('in',$province),
            '_logic' => 'or'
        );

        return $AreaMap;

    }

    /**
     * 查看
     */
    public function view() {
        if (IS_GET) {
            $id = I('get.id',0);
            $map = array('id'=>$id);
            $result = apiCall(OrdersInfoViewApi::GET_INFO, array($map));
            if ($result['status']) {
                $order_code = $result['info']['order_code'];
                $this -> assign("order", $result['info']);
                $result = apiCall(OrdersItemApi::QUERY_NO_PAGING, array(array('order_code'=>$order_code)));
                if(!$result['status']){
                    ifFailedLogRecord($result, __FILE__.__LINE__);
                    $this->error($result['info']);
                }

                $this -> assign("items", $result['info']);

                //查询订单状态变更纪录
                $result = apiCall(OrderStatusHistoryApi::QUERY_NO_PAGING, array(array('order_code'=>$order_code),"create_time desc"));
//				dump($result);
                if(!$result['status']){
                    ifFailedLogRecord($result, __FILE__.__LINE__);
                    $this->error($result['info']);
                }

                $this -> assign("statushistory", $result['info']);
                $this -> display();
            } else {
                $this -> error($result['info']);
            }
        }
    }
}